import {
  _decorator,
  Component,
  SpriteFrame,
  Sprite,
  UITransform,
  Vec3,
  tween,
} from "cc";
import { LoadPictures } from "db://assets/Scripts/Utils/LoadPictures";
import {
  MovingCommand,
  TurnDirectionCommand,
  ClockWiseAngles,
  Orientation,
} from "db://assets/Scripts/Enums/Events/ActionsEnum";
import { CustomEventListener } from "db://assets/Scripts/Listener/CustomEventListener";
import { PlayerMovingAction } from "db://assets/Scripts/Action/PlayerMovingAction";
import { PlayerDataInterFace } from "db://assets/Scripts/Entity/PlayerDataInterFace";
import { PlayerKeyBoardAssist } from "db://assets/Scripts/Assist/PlayerKeyBoardAssist";
const { ccclass, property } = _decorator;

@ccclass("PlayerManager")
export class PlayerManager extends Component {
  movCmdComp: PlayerMovingAction;
  keyAssist: PlayerKeyBoardAssist;
  loadPic: LoadPictures = LoadPictures.getInstance();

  chriotImgPath = "player-chriot/up/spriteFrame";
  playerSpriteFrame: SpriteFrame = null;

  speed: number = 1;
  playerSprite: Sprite = null;
  coordinate: Vec3 = new Vec3();
  playerData: PlayerDataInterFace;
  currentAngle: number = 0;
  presentDirection: any = Orientation.TOP;

  controlMoving(command: string) {
    if (
      command === TurnDirectionCommand.TURN_CLOCK_90 ||
      command === TurnDirectionCommand.TURN_COUNTER_CLOCK_90
    ) {
      this.changeDirection(command);
    } else if (
      command === MovingCommand.FORWARD_MOVE ||
      command === MovingCommand.BACKWARD_MOVE
    ) {
      this.verticalMovement(command);
    } else if (
      command === MovingCommand.LEFT_MOVE ||
      command === MovingCommand.RIGHT_MOVE
    ) {
      this.horizontalMovement(command);
    }
  }

  horizontalMovement(dictate: string) {
    //TODO
    this.playerData = {
      command: dictate,
      targetNode: this.node,
      speed: this.speed,
      presentDirection: this.presentDirection,
      coordinate: this.coordinate,
    };
    this.movCmdComp.moveByCommand(this.playerData);
  }

  obtainOrientation(angleParam: number) {
    // 确保角度在0到360之间
    let angleVal = angleParam % 360;
    // 处理负角度,将其转换为等价的正角度
    while (angleVal < 0) {
      angleVal += 360; // 将负角度转换为正角度
    }
    let orientation = null;

    // 根据角度确定朝向
    if (angleVal === 0) {
      orientation = Orientation.TOP;
    } else if (angleVal === 90) {
      orientation = Orientation.RIGHT;
    } else if (angleVal === 180) {
      orientation = Orientation.DOWN;
    } else if (angleVal === 270) {
      orientation = Orientation.LEFT;
    } else {
      orientation = Orientation.OTHER; // 如果不是标准的90度倍数
    }
    // console.log("朝向:", orientation, "; angleVal:", angleVal);
    return orientation;
  }

  changeDirection(dictate: string) {
    const triggerCount = 6;

    if (dictate === TurnDirectionCommand.TURN_CLOCK_90) {
      let angleNum =
        this.currentAngle + ClockWiseAngles.COUNTER_CLOCK_WISE / triggerCount;
      // console.log("angleNum:", angleNum);
      this.currentAngle = angleNum;

      tween(this.node).to(1, { angle: angleNum }).start();
      this.presentDirection = this.obtainOrientation(angleNum);
    } else if (dictate === TurnDirectionCommand.TURN_COUNTER_CLOCK_90) {
      let angleNum2 =
        this.currentAngle + ClockWiseAngles.CLOCK_WISE / triggerCount;
      // console.log("angleNum2:", angleNum2);
      this.currentAngle = angleNum2;

      tween(this.node).to(1, { angle: angleNum2 }).start();
      this.presentDirection = this.obtainOrientation(angleNum2);
    }
    console.log("this.presentDirection=", this.presentDirection);
  }

  verticalMovement(dictate: string) {
    this.playerData = {
      command: dictate,
      targetNode: this.node,
      speed: this.speed,
      presentDirection: this.presentDirection,
      coordinate: this.coordinate,
    };
    this.movCmdComp.moveByCommand(this.playerData);
  }

  bindListenerMethod() {
    const listener = CustomEventListener.getInstance();
    listener.on(MovingCommand.FORWARD_MOVE, this.controlMoving, this);
    listener.on(MovingCommand.BACKWARD_MOVE, this.controlMoving, this);
    listener.on(MovingCommand.LEFT_MOVE, this.controlMoving, this);
    listener.on(MovingCommand.RIGHT_MOVE, this.controlMoving, this);

    listener.on(TurnDirectionCommand.TURN_CLOCK_90, this.controlMoving, this);
    listener.on(
      TurnDirectionCommand.TURN_COUNTER_CLOCK_90,
      this.controlMoving,
      this
    );
  }

  async initializeMethod() {
    this.playerSpriteFrame = await this.loadPic.lodeSingleFile(
      this.chriotImgPath
    );

    this.playerSprite = this.node.addComponent(Sprite);
    this.playerSprite.spriteFrame = this.playerSpriteFrame;

    this.initializeUIMethod();
  }

  initializeUIMethod() {
    const transform = this.node.getComponent(UITransform);
    transform.setContentSize(40, 50);
  }

  start() {
    this.initializeMethod();
    this.keyAssist.register(this);
    console.log(this);
  }

  onLoad() {
    this.movCmdComp = this.node.addComponent(PlayerMovingAction);
    this.keyAssist = this.getComponent(PlayerKeyBoardAssist);
    this.bindListenerMethod();
  }
}
